package com.fanjia.crm.service.impl;

import com.fanjia.crm.mapper.RoleMapper;
import com.fanjia.crm.model.Role;
import com.fanjia.crm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by FanJia on 2020/9/21 21:03
 */
@Service
@Transactional
public class RoleServiceImpl implements IRoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<Role> findAllRoles() {
        return roleMapper.findAllRoles();
    }

    @Override
    public void insert(Role role, String[] ids) {
        if(role.getId()==0){
            System.out.println("保存角色");
            roleMapper.insert(role);
            System.out.println(role);

        }else{
            System.out.println("修改角色");
            //修改名称，描述
            roleMapper.updateRoleByRole(role);
            //清除权限
            roleMapper.deletePermissionByRoleId(role.getId());
            //再重新添加新的权限
        }
        //添加权限
//        for(String pmsId : ids){
////            roleMapper.insertRolePermission(role.getId(),Integer.parseInt(pmsId));
////
////        }
            roleMapper.insertRolePermissionBatch(role.getId(),ids);


    }

    @Override
    public Role findByRoleId(Integer roleId) {
        return roleMapper.findByRoleId(roleId);
    }

    @Override
    public String findPermissionIdsByRoleId(Integer roleId) {
       List<Integer> idsList= roleMapper.findPermissionIdsByRoleId(roleId);
        StringBuffer sb = new StringBuffer("");
        if(idsList.size() == 0){
            return "";
        }
        for(int id : idsList){
            sb.append(id).append(",");
        }
        sb.deleteCharAt(sb.length()-1);


        return sb.toString();
    }
}
